home *** CD-ROM | disk | FTP | other *** search
/ The Best of MacTutor - S…e Code for Volumes 1 to 5 / The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin / Source Code / #40 (Jan 89) / FinderControls.sit / Controls texte < prev    next >
Text File  |  1988-04-23  |  5KB  |  186 lines

  1. {**********************************************}
  2. { Put this file in the project after MacPasLib and MacTraps. }
  3. { Don't forget to "Use resource file" in "Run options" of menu "Project" }
  4. { This resource file may be empty, or may contain a copy of }
  5. { system file reources CDEF 0 and 1 }
  6. {**********************************************}
  7. PROGRAM Controls;
  8.  
  9.     CONST
  10.         CDEFID = 1;        { 0 for button, 1 for scroll bar }
  11.  
  12.     TYPE
  13.         CDEFcodeHdl = ^CDEFcodePtr;
  14.         CDEFcodePtr = ^CDEFcodeRecord;
  15.         CDEFcodeRecord = RECORD
  16.                 jump : integer;
  17.                 address : ProcPtr;
  18.             END;
  19.  
  20.     VAR
  21.         boundsRect, TextRect : rect;
  22.         myControl, whichControl : ControlHandle;
  23.         theEvent : eventRecord;
  24.         thePoint : point;
  25.         whichPart : integer;
  26.         theWindow, whichWindow : WindowPtr;
  27.         myFakeCDEF : CDEFcodeHdl;
  28.         myFakeCDEFid : integer;
  29.         CDEFProcHandle : handle;
  30.  
  31.  
  32.     LABEL
  33.         1, 2;
  34.  
  35.  
  36.  
  37.     FUNCTION DoCDEF (varCode : integer;
  38.                                     theControl : ControlHandle;
  39.                                     message : integer;
  40.                                     param : longint;
  41.                                     ProcHandle : handle) : longint;
  42.     INLINE
  43. { CDEF relies on a prepared default value of 0 as function result : }
  44.         $42AF, $0010,     {CLR.L 10(A7)}
  45.  
  46. { JSR to a procedure passed by handle as last argument }
  47.         $205F,            {MOVE.L (A7)+,A0}
  48.         $2050,            {MOVE.L (A0),A0}
  49.         $4E90;            {JSR (A0)}
  50.  
  51.  
  52.  
  53.     FUNCTION ControlProc (varCode : integer;
  54.                                     theControl : ControlHandle;
  55.                                     message : integer;
  56.                                     param : longint) : longint;
  57.     BEGIN
  58.         writeln('     message= ', message, 'param= ', param);
  59.         ControlProc := DoCDEF(varCode, theControl, message, param, CDEFProcHandle);
  60.     END;
  61.  
  62.  
  63.  
  64. BEGIN
  65. { create a intermediate CDEF resource : }
  66.     myFakeCDEF := CDEFcodeHdl(NewHandle(sizeof(CDEFcodeRecord)));
  67.     IF MemError <> NoErr THEN
  68.         GOTO 2;
  69.     myFakeCDEF^^.jump := $4EF9;
  70.     myFakeCDEF^^.address := @ControlProc;
  71.  
  72.     REPEAT
  73.         myFakeCDEFid := UniqueID('CDEF');
  74. { in order to have 16*myFakeCDEFid < maxint : }
  75.     UNTIL myFakeCDEFid < 1000;
  76.  
  77.     AddResource(handle(myFakeCDEF), 'CDEF', myFakeCDEFid, '');
  78.     IF ResError <> NoErr THEN
  79.         GOTO 2;
  80.  
  81.     SetRect(TextRect, 250, 40, 500, 330);
  82.     SetTextRect(TextRect);
  83.     ShowText;
  84.     SetRect(boundsRect, 40, 40, 200, 200);
  85.     theWindow := NewWindow(NIL, boundsrect, 'my window', true, 0, pointer(-1), false, 0);
  86.     SetPort(theWindow);
  87.     CDEFProcHandle := GetResource('CDEF', CDEFID);
  88.     IF ResError <> NoErr THEN
  89.         GOTO 1;
  90.     HLock(CDEFProcHandle);
  91.     IF MemError <> NoErr THEN
  92.         GOTO 1;
  93.     SetRect(boundsRect, 10, 10, 90, 26);
  94.  
  95. { now we begin to test the Control Manager's routines : }
  96.  
  97.     writeln('NewControl : ');
  98.     myControl := NewControl(thePort, boundsRect, 'my control', true, 0, 0, 48, myFakeCDEFid * 16, 0);
  99.     SetCtlAction(myControl, pointer(-1));
  100.     writeln('SetCTitle : ');
  101.     SetCTitle(myControl, 'new name');
  102.     writeln('HideControl : ');
  103.     HideControl(myControl);
  104.     writeln('ShowControl : ');
  105.     ShowControl(myControl);
  106.     writeln('HiliteControl : ');
  107.     HiliteControl(myControl, 1);
  108.     writeln('HiliteControl : ');
  109.     HiliteControl(myControl, 255);
  110.     writeln('HiliteControl : ');
  111.     HiliteControl(myControl, 0);
  112.  
  113.     writeln('SetCtlValue : ');
  114.     SetCtlValue(myControl, 1);
  115.     writeln('SetCtlMax : ');
  116.     SetCtlMax(myControl, 0);
  117.     writeln('SetCtlMax : ');
  118.     SetCtlMax(myControl, 1);
  119.     writeln('SetCtlMin : ');
  120.     SetCtlMin(myControl, 1);
  121.     writeln('SetCtlMin : ');
  122.     SetCtlMin(myControl, 0);
  123.     writeln('SetCtlValue : ');
  124.     SetCtlValue(myControl, 0);
  125.  
  126.     writeln('MoveControl : ');
  127.     MoveControl(myControl, 20, 20);
  128.     writeln('SizeControl : ');
  129.     SizeControl(myControl, 100, 30);
  130.  
  131. { let's have a loop to test "TrackControl" (and "FindControl") : }
  132.     writeln('The user should try actions on the control.');
  133.     writeln('End by clicking outside the control.');
  134.     FlushEvents(EveryEvent, 0);
  135.     InitCursor;
  136.     REPEAT
  137.         REPEAT
  138.         UNTIL GetNextEvent(MDownMask, theEvent);
  139.         thePoint := theEvent.where;
  140.         whichPart := FindWindow(thePoint, whichWindow);
  141.         SetPort(whichWindow);
  142.         GlobalToLocal(thePoint);
  143.         writeln('FindControl : ');
  144.         whichPart := FindControl(thePoint, whichWindow, whichControl);
  145.         IF whichControl <> NIL THEN
  146.             BEGIN
  147.                 writeln('TrackControl : ');
  148.                 whichPart := TrackControl(whichControl, thePoint, pointer(-1));
  149.             END;
  150.     UNTIL whichControl = NIL;
  151.  
  152.     FlushEvents(MUpMask, 0);
  153. { let's have another loop to test "DragControl" (and "FindControl") : }
  154.     writeln('The user should try to drag the control.');
  155.     writeln('End by clicking outside the control.');
  156.     REPEAT
  157.         REPEAT
  158.         UNTIL GetNextEvent(MDownMask, theEvent);
  159.         thePoint := theEvent.where;
  160.         whichPart := FindWindow(thePoint, whichWindow);
  161.         SetPort(whichWindow);
  162.         GlobalToLocal(thePoint);
  163.         writeln('FindControl : ');
  164.         whichPart := FindControl(thePoint, whichWindow, whichControl);
  165.         IF whichControl <> NIL THEN
  166.             WITH whichWindow^ DO
  167.                 BEGIN
  168.                     writeln('DragControl : ');
  169.                     DragControl(whichControl, thePoint, PortRect, PortRect, noConstraint);
  170.                 END;
  171.     UNTIL whichControl = NIL;
  172.  
  173.     writeln('DisposControl : ');
  174.     DisposeControl(myControl);
  175.     writeln('end');
  176.  
  177. 1 :
  178. { let's remove our fake CDEF : }
  179.     RmveResource(handle(myFakeCDEF));
  180.     IF ResError <> NoErr THEN
  181.         GOTO 2;
  182.     DisposHandle(handle(myFakeCDEF));
  183.  
  184. { error label : }
  185. 2 :
  186. END.